<template>
  <el-dialog :title="userTitle" :visible.sync="userShow" :before-close="close" width="55%">
    <el-form ref="userAdd" :model="userAdd" :rules="rules" :inline="true" label-position="left" style="margin-left:10px">
      <el-row>
        <el-col :span="10">
          <el-form-item label="工号" prop="code" :label-width="formLabelWidth">
            <el-input v-model="userAdd.code" autocomplete="off" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="用户名" prop="name" :label-width="formLabelWidth">
            <el-input v-model="userAdd.name" autocomplete="off" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="10">
          <el-form-item label="性别" prop="sex" :label-width="formLabelWidth">
            <template>
              <div style="width: 100%; display: inline-block">
                <el-radio v-model="userAdd.sex" :label="0">男</el-radio>
                <el-radio v-model="userAdd.sex" :label="1">女</el-radio>
              </div>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="密码" prop="password" :label-width="formLabelWidth">
            <el-input v-model="userAdd.password" autocomplete="off" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="10">
          <el-form-item label="所属部门" prop="departmentId" :label-width="formLabelWidth">
            <el-select v-model="userAdd.departmentId" placeholder="请选择" style="width: 100%">
              <el-option
                v-for="item in optionsDepartment"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="职位" prop="positionId" :label-width="formLabelWidth">
            <el-select v-model="userAdd.positionId" placeholder="请选择" style="width: 100%">
              <el-option
                v-for="item in optionsPosition"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="10">
          <el-form-item label="电话" prop="tel" :label-width="formLabelWidth">
            <el-input v-model="userAdd.tel" autocomplete="off" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="邮箱" prop="email" :label-width="formLabelWidth">
            <el-input v-model="userAdd.email" autocomplete="off" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="10">
          <el-form-item label="拥有角色" prop="roleList" :label-width="formLabelWidth">
            <el-select v-model="userAdd.roleList" multiple placeholder="请选择" style="width: 100%" @change="changeList">
              <el-option
                v-for="item in optionsRole"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="其他联系" prop="other" :label-width="formLabelWidth">
            <el-input v-model="userAdd.other" autocomplete="off" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="10">
          <el-form-item label="生日" prop="birthday" :label-width="formLabelWidth">
            <el-date-picker
              v-model="userAdd.birthday"
              type="date"
              placeholder="选择日期"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="备注" prop="remark" :label-width="formLabelWidth">
            <el-input v-model="userAdd.remark" autocomplete="off" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="add">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'UserDialog',
  props: {
    userTitle: String,
    userShow: Boolean,
    userDialogData: Object,
    optionsDepartment: Array,
    optionsPosition: Array,
    optionsRole: Array,
    userClose: Function,
    userClick: Function
  },
  data() {
    return {
      formLabelWidth: '80px',
      userAdd: {},
      initData: {}, // 中间值
      rules: {
        // 输入限制
        name: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' },
          {
            pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/,
            message: '密码由6-20位字母和数字组成'
          }
        ],
        code: [{ required: true, message: '请输入工号', trigger: 'blur' }],
        sex: [{ required: true, message: '请选择性别', trigger: 'blur' }],
        tel: [{ required: true, message: '请输入电话', trigger: 'blur' },
          {
            pattern: /^((0\d{2,3}-\d{7,8})|(1[3|4|5|7|8][0-9]{9}))$/,
            message: '请输入正确的手机号码或者座机号'
          }
        ],
        departmentId: [{ required: true, message: '请选择部门', trigger: 'blur' }],
        positionId: [{ required: true, message: '请选择职位', trigger: 'blur' }],
        roleList: [{ required: true, message: '请选择角色', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 数据初始化
    onShow() {
      this.userAdd = this.userDialogData
    },
    // 确认按钮
    add() {
      this.$refs.userAdd.validate((valid) => {
        if (valid) {
          this.userClick(this.userAdd)
        } else {
          console.log('输入错误')
          return false
        }
      })
    },
    // 关闭按钮
    close() {
      this.userClose()
      this.$refs.userAdd.resetFields()
    },
    // 角色多选
    changeList(selectData) {
      this.userAdd.roleList = selectData
    }
  }
}
</script>

